package com.zp.mall.product.service.impl;

import com.zp.common.to.SpuBoundsTo;
import com.zp.common.utils.R;
import com.zp.mall.product.dao.SpuInfoDescDao;
import com.zp.mall.product.entity.*;
import com.zp.mall.product.feign.CouponFeignService;
import com.zp.mall.product.service.*;
import com.zp.mall.product.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zp.common.utils.PageUtils;
import com.zp.common.utils.Query;

import com.zp.mall.product.dao.SpuInfoDao;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Autowired
    SpuInfoDescDao spuInfoDescDao;
    @Autowired
    SpuImagesService spuImagesService;
    @Autowired
    AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    CouponFeignService couponFeignService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page (
                new Query<SpuInfoEntity> ().getPage (params),
                new QueryWrapper<SpuInfoEntity> ()
        );

        return new PageUtils (page);
    }

    /**
     * 保存商品
     *
     * @param vo
     */
    @Override
    @Transactional
    public void saveSpuInfoVo(SpuInfoVo vo) {
        // 1、保存Spu基本信息   pms_spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity ();
        BeanUtils.copyProperties (vo, spuInfoEntity);
        spuInfoEntity.setCreateTime (new Date ());
        spuInfoEntity.setUpdateTime (new Date ());
        this.save (spuInfoEntity);

        Long spuId = spuInfoEntity.getId ();
        // 2、保存Spu描述图片   pms_spu_info_desc
        List<String> decript = vo.getDecript ();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity ();
        spuInfoDescEntity.setSpuId (spuId);
        spuInfoDescEntity.setDecript (String.join (",", decript));
        spuInfoDescDao.insert (spuInfoDescEntity);

        // 3、保存Spu图片集   pms_spu_images
        List<String> images = vo.getImages ();
        if (images != null && images.size () > 0) {
            List<SpuImagesEntity> collect = images.stream ().map (img -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity ();
                spuImagesEntity.setSpuId (spuId);
                spuImagesEntity.setImgUrl (img);
                return spuImagesEntity;
            }).collect (Collectors.toList ());
            spuImagesService.saveBatch (collect);
        }


        // 4、保存Spu积分信息   gulimall_sms -> sms_spu_bounds   远程调用
        Bounds bounds = vo.getBounds ();
        SpuBoundsTo spuBoundsTo = new SpuBoundsTo ();
        BeanUtils.copyProperties (bounds, spuBoundsTo);
        spuBoundsTo.setSpuId (spuId);
        R r = couponFeignService.save (spuBoundsTo);
        if (r.getCode (r) != 0) {
            throw new RuntimeException ("保存Spu积分失败");
        }


        // 5、保存Spu的规格参数   pms_product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs ();
        if (baseAttrs != null && baseAttrs.size () > 0) {
            List<ProductAttrValueEntity> collect = baseAttrs.stream ().map (baseAttr -> {
                ProductAttrValueEntity valueEntity = new ProductAttrValueEntity ();
                BeanUtils.copyProperties (baseAttr, valueEntity);
                valueEntity.setSpuId (spuId);
                AttrEntity attrEntity = attrService.getById (valueEntity.getAttrId ());
                valueEntity.setAttrName (attrEntity.getAttrName ());
                return valueEntity;
            }).collect (Collectors.toList ());
            productAttrValueService.saveBatch (collect);
        }

        // 6、保存Sku信息
        List<Skus> skus = vo.getSkus ();
        skus.forEach (sku -> {
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity ();
            BeanUtils.copyProperties (sku, skuInfoEntity);
            skuInfoEntity.setCatalogId (vo.getCatalogId ());
            skuInfoEntity.setBrandId (vo.getBrandId ());
            skuInfoEntity.setSpuId (spuId);
            skuInfoEntity.setSaleCount (0L);
            skuInfoEntity.setSkuDesc (String.join (" ", sku.getDescar ()));
            List<Images> imagesList = sku.getImages ();
            if (imagesList != null && imagesList.size () > 0) {
                for (Images images1 : imagesList) {
                    if (images1.getDefaultImg () == 1) {
                        skuInfoEntity.setSkuDefaultImg (images1.getImgUrl ());
                        break;
                    }
                }
            }
            // 6.1、保存Sku基本信息    pms_sku_info
            skuInfoService.save (skuInfoEntity);

            Long skuId = skuInfoEntity.getSkuId ();

            // 6.2、保存Sku展示图片  pms_sku_images
            if (imagesList != null && imagesList.size () > 0) {
                List<SkuImagesEntity> imagesEntities = imagesList.stream ().filter (img -> {
                    return !StringUtils.isEmpty (img.getImgUrl ());
                }).map (img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity ();
                    skuImagesEntity.setSkuId (skuId);
                    skuImagesEntity.setImgUrl (img.getImgUrl ());
                    skuImagesEntity.setDefaultImg (img.getDefaultImg ());
                    return skuImagesEntity;
                }).collect (Collectors.toList ());

                if (imagesEntities != null && imagesEntities.size () > 0) {
                    skuImagesService.saveBatch (imagesEntities);
                }

            }

            // 6.3、保存销售属性值信息   pms_sku_sale_attr_value
            List<Attr> attr = sku.getAttr ();
            if (attr != null && attr.size () > 0){
                List<SkuSaleAttrValueEntity> collect = attr.stream ().map (a -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity ();
                    BeanUtils.copyProperties (a, skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setSkuId (skuId);
                    return skuSaleAttrValueEntity;
                }).collect (Collectors.toList ());
                skuSaleAttrValueService.saveBatch (collect);
            }


            // 6.4、sku的优惠、满减、会员价格等  gulimall_sms -》 sms_sku_ladder 、 sms_sku_full_reduction、 sms_member_price

        });


    }

}